<script type="text/javascript">
	function doSearchGoods(value) {
		
	}

	//表格查询  
	function searchGoods() {
		var params = goods_list.datagrid('options').queryParams; //先取得 datagrid 的查询参数  
		printLog(params);
		goods_list.datagrid('reload'); //设置好查询参数 reload 一下就可以了  
		goods_list.datagrid('unselectAll');
		goods_list.datagrid('uncheckAll');
	}
	
	//表格查询  
	function reloadGoods() {
		var params = goods_list.datagrid('options').queryParams; //先取得 datagrid 的查询参数  
		printLog(params);
		goods_list.datagrid('reload'); //设置好查询参数 reload 一下就可以了  
		goods_list.datagrid('unselectAll');
		goods_list.datagrid('uncheckAll');
	}

	function addGoods() {
		createFromWindowOptions({
			title:"新增商品信息",
			maximizable:false,
			href:'goods.do?editGoods',
			callback:function(d) {
				var f  = d.find('#editGoodsForm');
				var validate = f.form("validate");
				if (validate) {
					f.submit();
	            } else {
	                return false;
	            }
			},
			enableSaveButton:true
		});
// 		createFromWindow("新增商品信息", "goods.do?editGoods", "goods.do?saveGoods",
// 				function(data) {
// 					alert(data);
// 					reloadGoods();
// 				});
		
// 		var width = width?width:700;
// 		var height = height?height:400;
// 		$.dialog({
// 			content: 'url:goods.do?editGoods',
// 			lock : true,
// 			width:width,
// 			height:height,
// 			title:'新增商品信息',
// 			opacity : 0.3,
// 			cache:false,
// 		    ok: function(){
// 		    	iframe = this.iframe.contentWindow;
// // 				saveObj();
// 				return false;
// 		    },
// 		    cancelVal: '关闭',
// 		    cancel: true /*为true等价于function(){}*/
// 		});
	}

	function editGoods() {
		var rows = goods_list.datagrid('getChecked');
		printLog(rows);
		if (rows&&rows.length>0) {
			if(rows.length>1) {
				showMsg('请选择一条操作记录.');
				return;
			}
// 			createFromWindow("编辑商品信息", "goods.do?editGoods&goodsId=" + rows[0].id,
// 					"goods.do?saveGoods", function(data) {
// 						reloadGoods();
// 					},true);
			createFromWindowOptions({
				title:"编辑信息",
				maximizable:false,
				href:"goods.do?editGoods&goodsId=" + rows[0].id,
				callback:function(d) {
					var f  = d.find('#editGoodsForm');
					var validate = f.form("validate");
					if (validate) {
						f.submit();
		            } else {
		                return false;
		            }
				},
				enableSaveButton:true
			});
		} else {
			showMsg('请选择操作记录.');
		}
	}
	
	function deleteGoods() {
		var rows = goods_list.datagrid('getChecked');
		printLog(rows);
	    if (rows&&rows.length>0){
	        $.messager.confirm('询问','你确认要删除商品吗?',function(r){
	            if (r){
	            	var ids = "";
	            	for(var i=0; i<rows.length; i++){
	            		ids += rows[i].id+",";
	            	}
	                $.post('goods.do?deleteGoodsItems',{ids:ids},function(result){
	                    if (result.success){
	                    	reloadGoods();    
	                    }
	                    showMsg(result.msg);
	                },'json');
	            }
	        });
	    }else {
	    	showMsg("请选择操作记录.");
	    }
	}
	//上架商品
	function putawayGoods() {
		var rows = goods_list.datagrid('getChecked');
		printLog(rows);
	    if (rows&&rows.length>0){
	        $.messager.confirm('询问','你确认要上架商品吗?',function(r){
	            if (r){
	            	var ids = "";
	            	for(var i=0; i<rows.length; i++){
	            		ids += rows[i].id+",";
	            	}
	                $.post('goods.do?putawayGoodsItems',{ids:ids},function(result){
	                    if (result.success){
	                    	reloadGoods();    
	                    }
	                    showMsg(result.msg);
	                },'json');
	            }
	        });
	    }else {
	    	showMsg("请选择操作记录.");
	    }
	}
	//下架商品
	function soldOutGoods() {
		var rows = goods_list.datagrid('getChecked');
		printLog(rows);
	    if (rows&&rows.length>0){
	        $.messager.confirm('询问','你确认要下架商品吗?',function(r){
	            if (r){
	            	var ids = "";
	            	for(var i=0; i<rows.length; i++){
	            		ids += rows[i].id+",";
	            	}
	                $.post('goods.do?soldOutGoodsItems',{ids:ids},function(result){
	                    if (result.success){
	                    	reloadGoods();    
	                    }
	                    showMsg(result.msg);
	                },'json');
	            }
	        });
	    }else {
	    	showMsg("请选择操作记录.");
	    }
	}
	
	//商品查询
	function goodsSearch() {
		setGoodsSearchParams(false);
	    reloadGoods(); 
	}
	//清空查询条件
	function clearGoodsSearch() {
		$('#goodsSearchForm').form('clear');
		setGoodsSearchParams(true);
		reloadGoods(); 
	}
	//设置查询参数或清空查询参数
	function setGoodsSearchParams(isClear) {
		var fields =$('#goodsSearchForm').serializeArray(); //自动序列化表单元素为JSON对象  
		var params = $('#goods_list').datagrid('options').queryParams; //先取得 datagrid 的查询参数  
	    $.each(fields, function(i, field){ 
	    	if(isClear) {
	    		delete params[field.name];
	    	}else {
	    		params[field.name] = field.value; //设置查询参数
	    	}
	    });
	    printLog(params);
	}
</script>

<div class="easyui-layout" data-options="fit:true">
	<div data-options="region:'center',border:false">
		<@eu.datagrid id="goods_list" toolbar="#goodslist_search" idField="id" operations="${operations!}"
			 url="goods.do?loadList"
			 item="com.xdtech.shop.vo.GoodsItem"/>
		
		<div id="goodslist_search">
			<div style="margin-bottom:5px;padding-left:5px;margin-top: 5px;">
				<form id="goodsSearchForm">
					<table>
						<tr>
							<td>商品名称：</td>
							<td><input type="text" name="name" style="width: 150px" /></td>
							<td>商品状态：</td>
							<td><select class="easyui-combobox" name="status" style="width:150px;">  
								<option value="">------</option> 
							    <option value="0">下架</option>  
							    <option value="1">上架</option>
							</select> </td>
							<td>商品类别：</td>
							<td><@eu.comboTree url="category.do?categoryTree" name="categoryIds" multiple="true"/></td>
							<td>
								<@eu.linkbutton showName="查询" iconCls="icon-search" onclick="goodsSearch()"/>
	 					 		<@eu.linkbutton showName="清空" iconCls="icon-clear" onclick="clearGoodsSearch()"/>
 					 		</td>
						</tr>
					</table>
					 
				</form>
			</div>
			<div id="goods_toolbar" style="height: auto">
				<@eu.linkbutton showName="新增" shiro="shop:goods:add" iconCls="icon-add" onclick="addGoods()"/>
				<@eu.linkbutton showName="上架" shiro="shop:goods:putaway" iconCls="icon-remove" onclick="putawayGoods()"/>
				<@eu.linkbutton showName="下架" shiro="shop:goods:soldOut" iconCls="icon-remove" onclick="soldOutGoods()"/>
				<@eu.linkbutton showName="删除" shiro="shop:goods:delete" iconCls="icon-remove" onclick="deleteGoods()"/>
				<@eu.linkbutton showName="编辑" shiro="shop:goods:edit" iconCls="icon-edit" onclick="editGoods()"/>
				<@eu.linkbutton showName="刷新" shiro="shop:goods:refresh" iconCls="icon-reload" onclick="searchGoods()"/>
			</div>
		</div>
	</div>
</div>